$(function() {
	var $win = $('#edit-window').window({
		title : '新增',
		width : 430,
		height : 420,
		top : ($(window).height() - 430) * 0.5,
		left : ($(window).width() - 420) * 0.5,
		shadow : true,
		modal : true,
		closed : true,
		minimizable : false,
		maximizable : false,
		collapsible : false
	});
	
	var form = $win.find("form");
	
	$("#btn_repwd").click(function() {
		var rows = $('#datagrid').datagrid('getSelections');
    	if(rows.length == 0 || rows.length > 1){
    		$.messager.alert('警告', '请选择一条记录!', 'warning');
    		return;
    	}
		$.messager.confirm("操作提示","您确定要重置密码吗？",function(data) {
			if (data) {
				$.ajax({
					url : '/AQAppServer/account/resetPwdWeb',
					type : 'POST',
					dataType:'json',
					data:{'phoneNum':rows[0].phoneNum},
					success : function(data) {
						$("#datagrid").datagrid('reload');
						$.messager.alert('成功', '操作成功', 'info');
					}
		    	});
			}
		});
	});
	
	$("#btn_speak").click(function() {
		var rows = $('#datagrid').datagrid('getSelections');
    	if(rows.length == 0 || rows.length > 1){
    		$.messager.alert('警告', '请选择一条记录!', 'warning');
    		return;
    	}
    	var status;
    	if(rows[0].status=='0')
    	{
    		status = 1;
    	}
    	else
    	{
    		status = 0;
    	}
    	$.ajax({
			url : '/AQAppServer/account/updateSpeakWeb',
			type : 'POST',
			dataType:'json',
			data:{'status':status,'phoneNum':rows[0].phoneNum},
			success : function(data) {
				$("#datagrid").datagrid('reload');
				$.messager.alert('成功', '操作成功', 'info');
			}
    	});
	});
	
	$("#btn_feather").click(function(){
		form.form("clear");
    	var rows = $('#datagrid').datagrid('getSelections');
    	if(rows.length == 0 || rows.length > 1){
    		$.messager.alert('警告', '请选择一条记录!', 'warning');
    		return;
    	}
    	$('#edit_nickName').textbox('setValue',rows[0].nickName);
		$('#edit_phoneNum').textbox('setValue',rows[0].phoneNum);
		
		$win.window('open');
		$win.window('setTitle','修改');
		form.url = '/AQAppServer/account/modAccount';
	});
	
	
	$("#search").click(function() {
		var queryParams = $('#datagrid').datagrid('options').queryParams;
		queryParams.nickName = $('#search_nickName').val();
		queryParams.phoneNum = $('#search_phoneNum').val();
		var page = $("#datagrid").datagrid('getPager');
		queryParams.start = page.pagination('options').pageNumber;
		queryParams.limit = page.pagination('options').pageSize;
		page.pagination('select', 1);
	});
	
	$("#add").click(function() {
		$win.window('open');
		$('#edit_loginName').textbox('setValue','');
		$('#edit_credit').textbox('setValue','');
		$('#edit_mobile').textbox('setValue','');
		$('#edit_wx').textbox('setValue','');
		$('#edit_qq').textbox('setValue','');
		$('#edit_headImg').textbox('setValue','');
		$('#edit_status').combobox('setValue','0');
		form.url = '/AQAppServer/account/addAccount';
	});
	
	$("#submitForm").click(function() {
		form.form('submit', {
			url : form.url,
			success : function(data) {
				data = toJson(data);
				if (data.flag == 0) {
					$("#datagrid").datagrid('reload');
					$win.window('close');
				} else {
					$.messager.alert('错误', data.flag, 'error');
				}
			}
		});
	});
	
	$("#clearForm").click(function() {
		form.form("clear");
	});
	
	$("#modify").click(function(){
		form.form("clear");
    	var rows = $('#datagrid').datagrid('getSelections');
    	if(rows.length == 0 || rows.length > 1){
    		$.messager.alert('警告', '请选择一条记录!', 'warning');
    		return;
    	}
    	$('#edit_loginName').textbox('setValue',rows[0].loginName);
		$('#edit_credit').textbox('setValue',rows[0].credit);
		$('#edit_mobile').textbox('setValue',rows[0].mobile);
		$('#edit_wx').textbox('setValue',rows[0].wx);
		$('#edit_qq').textbox('setValue',rows[0].qq);
		$('#edit_headImg').textbox('setValue',rows[0].headImg);
		$('#edit_status').combobox('setValue',rows[0].status);
		
		$win.window('open');
		$win.window('setTitle','修改');
		form.url = '/AQAppServer/account/modAccount';
	});
	
	$("#delete").click(function() {
		var rows = $('#datagrid').datagrid('getSelections');
		if (rows.length == 0) {
			$.messager.alert('警告', '请至少选择一条记录!', 'warning');
			return;
		}
		$.messager.confirm("操作提示","您确定要删除这些数据吗？",function(data) {
				if (data) {
					var accountIDs = '';
					for ( var i = 0; i < rows.length; i++) {
						if (i > 0) {
							accountIDs += ",";
						}
						accountIDs += rows[i].accountID;
					}
					$.ajax({
						url : '/AQAppServer/account/delAccount',
						type : 'POST',
						dataType: 'json',
						data: 'accountIDs=' + accountIDs,
						success : function(data) {
							data = toJson(data);
							if (data.flag == 0) {
								$("#datagrid").datagrid('reload');
							} else {
								$.messager.alert('错误',data.flag,'error');
							}
						},
						error : function() {
							$.messager.alert('错误','操作失败!','error');
						}
					});
				}
			});
	});
});